// pages/typegoods/typegoods.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      havebg: 1, //1表示显示    0表示不显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '商品列表', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    height: app.globalData.height,
  },
  out() {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    let typename = options.btname
    _this.setData({
      typename,
      type:options.type,
      banner:options.banner,
      id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this
    let user_info = wx.getStorageSync('user_info')
    if(_this.data.banner == 1){
      $api.getBannersdata({
        banner_id: _this.data.id,
        user_id:wx.getStorageSync('user_id'),
        login_token: wx.getStorageSync('login_token'),
      }).then((res) => {
  
        _this.setData({
          list: res.data.data.goods_info
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }else{
      $api.getindexzqdata({
        zq_id: _this.data.id,
        user_id:wx.getStorageSync('user_id'),
        login_token: wx.getStorageSync('login_token'),
      }).then((res) => {
  
        _this.setData({
          list: res.data.data
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }
   
  },
  scroll: function (e) {
    // console.log(e)
    this.setData({
      scrollTop: e.detail.scrollTop
    });
    if (e.detail.scrollTop >= 50) {
      this.setData({
        headerBg: true
      })
    } else {
      this.setData({
        headerBg: false
      })
    }
  },
  Readingdetails(e) {
    var _this = this;
    let is_register = e.currentTarget.dataset.is_register
    if(is_register == 2){
      wx.navigateTo({
        url: '/pages/Readingdetails/Readingdetails?pt=1&id=' + e.currentTarget.dataset.id,
      })
    }else if(is_register == 0){
      wx.navigateTo({
        url: '/pages/Readingdetails/Readingdetails?ep=1&id=' + e.currentTarget.dataset.id,
      })
    }
    
  },
  onpay(e){
    let _this = this
    wx.removeStorageSync('address')
    wx.removeStorageSync('applyShop')
    wx.removeStorageSync('wait_pay')
    wx.removeStorageSync('giftId')
    wx.removeStorageSync('pay')
    wx.removeStorageSync('goodsList')
    let goodinfo = e.currentTarget.dataset.item
    goodinfo.num = 1
    goodinfo.id = goodinfo.good_id
    let list = []
    list.push(goodinfo)
    list  = JSON.stringify(list)
    wx.navigateTo({
      url: '/pages/reading/reading?state=3&&twoType=1&&ptType=1&&goods=' + list + '&is_register=' + goodinfo.is_register,
    })
    wx.removeStorageSync('addressid')
  },
   // 加入购物车
   oncart(e){
    let data = e.currentTarget.dataset.item
    console.log(data)
    let buy_goods = [{
      good_id:data.good_id,
      num:1
    }]
    $api.addshoppingcart({
      buy_goods: JSON.stringify(buy_goods),
      mode:data.is_register,
      type:1
    }).then((res) => {
      if(res.data.code == 0){
        wx.showToast({
          title: '已加入购物车',
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  // 跳转购物车
  gocart(){
    wx.navigateTo({
      url: '/pages/cart/cart'
    })
  }
})